import { toRefs, type App } from "vue";
import { createI18n, type I18nOptions } from "vue-i18n";
import { langMessages } from "@/locales/index";
import { useDesignStoreWithout } from "@/stores/module/design";
import type { LocaleType } from "@/typings/locale";

type CreateI18nType = ReturnType<typeof createI18n>;

export let i18n: CreateI18nType;

// i18n 配置项
const createI18nOptions = (): I18nOptions => {
  const { langMode } = toRefs(useDesignStoreWithout());

  return {
    legacy: false,
    messages: langMessages,
    locale: langMode.value,
  };
};

export const setLocale = (lang: LocaleType) => {
  i18n.global.locale = lang;
  window.location.reload();
  // i18n.global.setLocaleMessage(lang, langMessages[lang]);
};

export const setupI18n = (app: App<Element>) => {
  const i18nOptions = createI18nOptions();
  i18n = createI18n(i18nOptions);
  app.use(i18n);
};
